package com.service;

import static com.def.GenericDef.ACTION_FLG_ADD;
import static com.def.GenericDef.ACTION_FLG_DELETE;
import static com.def.GenericDef.ACTION_FLG_INQUIRE;
import static com.def.GenericDef.ACTION_FLG_QUERY;
import static com.def.GenericDef.ACTION_FLG_UPDATE;
import static com.def.GenericDef.E_XPBISBOM_NOT_FOUND_RECORDER;
import static com.def.GenericDef.E_XPBISBOM_RECORDER_ALREADY_EXIST;
import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.def.GenericDef.RETURN_MESG_OK;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dao.IBisBomDao;
import com.dao.IBisDataDao;
import com.dao.IBisMdlDefDao;
import com.dao.IBisMtrlDao;
import com.icim.service.ICIMBaseService;
import com.model.Bis_bom;
import com.model.Bis_bomId;
import com.model.Bis_data;
import com.model.Bis_mdl_def;
import com.model.Bis_mtrl;
import com.tx.Xpbisbom.XpbisbomI;
import com.tx.Xpbisbom.XpbisbomIA;
import com.tx.Xpbisbom.XpbisbomO;
import com.tx.Xpbisbom.XpbisbomOA;
import com.util.GUIDGenerator;
import com.util.MqOpe;

@Scope("prototype")
@Service("XpbisbomService")
public class XpbisbomService extends ICIMBaseService{

	private Logger logger = Logger.getLogger(XpbisbomService.class);
	@Autowired
	private XpbisbomI xpbisbomI;

	@Autowired
	private XpbisbomO xpbisbomO;

	@Autowired
	private IBisBomDao bisBomDao;

	@Autowired
	private IBisMtrlDao bisMtrlDao;

	@Autowired
	private IBisMdlDefDao bisMdlDefDao;

	@Autowired
	private IBisDataDao bisDataDao;
	
	@Autowired
	private MqOpe mqOpe;

	public String subMainProc(String strInMsg) {
		logger.info("InTrx is " + strInMsg);
		try {
			xpbisbomI = (XpbisbomI) mqOpe.inStr2Obj(strInMsg, xpbisbomI);
			char action_flg = xpbisbomI.getAction_flg().charAt(0);
			switch (action_flg) {
			case ACTION_FLG_QUERY:
				xpbisbomO = queryByConditionFnc();
				break;
			case ACTION_FLG_ADD:
				xpbisbomO = addFnc();
				break;
			case ACTION_FLG_UPDATE:
				xpbisbomO = updateFnc();
				break;
			case ACTION_FLG_DELETE:
				xpbisbomO = deleteFnc();
				break;
			case ACTION_FLG_INQUIRE:
				queryByMtrl();
				break;
			default:
				xpbisbomO = errActionFnc();
			}
			if (RETURN_CODE_OK.equals(xpbisbomO.getRtn_code())) {
				bisBomDao.flushCurSession();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		} catch (Exception ex) {
			xpbisbomO.setTrx_id("XPBISBOM");
			xpbisbomO.setType_id("O");
			xpbisbomO.setRtn_code(RETURN_CODE_UNKNOWN);
			xpbisbomO.setRtn_mesg(ex.toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} finally {
			logger.info(mqOpe.obj2Str(xpbisbomO));
		}
		return mqOpe.obj2Str(xpbisbomO);
	}

	public XpbisbomO addFnc() {
		XpbisbomO xpbisbomO = new XpbisbomO();
		XpbisbomIA iary = xpbisbomI.getIary().get(0);
		Bis_bomId id = new Bis_bomId();

		String mtrl_prod_id_fk = iary.getMtrl_prod_id_fk();
		String mdl_id_fk = iary.getMdl_id_fk();

		xpbisbomO.setTrx_id("XPBISBOM");
		xpbisbomO.setType_id("O");

		id.setMdl_id_fk(mdl_id_fk);
		id.setMtrl_prod_id_fk(mtrl_prod_id_fk);
		id.setMdl_mtrl_seq_id(GUIDGenerator.javaGUID());

		Bis_bom queryBisQrs = bisBomDao.getDataByKeyProc(id);
		if (queryBisQrs != null) {
			xpbisbomO.setRtn_code(E_XPBISBOM_RECORDER_ALREADY_EXIST + "");
			xpbisbomO.setRtn_mesg("Record already Exist");
			return xpbisbomO;
		}
		Bis_bom bis_bom = new Bis_bom();
		bis_bom.setId(id);
		bis_bom.setOpe_id_fk(iary.getOpe_id_fk());
		bis_bom.setMtrl_unit_fk(iary.getMtrl_unit_fk());
		bis_bom.setCns_flg(iary.getCns_flg());
		bis_bom.setMnt_flg(iary.getMnt_flg());
		bis_bom.setMtrl_use_qty(iary.getMtrl_use_qty());//TODO 使用数量 如何写入
		bisBomDao.save(bis_bom);

		Bis_mtrl bis_mtrl = bisMtrlDao.get(mtrl_prod_id_fk);
		if (bis_mtrl == null) {
			xpbisbomO.setRtn_code(RETURN_CODE_UNKNOWN);
			xpbisbomO.setRtn_mesg("找不到料号:" + mtrl_prod_id_fk);
			return xpbisbomO;
		}
		Bis_mdl_def bis_mdl_def = bisMdlDefDao.get(mdl_id_fk);
		if (bis_mdl_def == null) {
			xpbisbomO.setRtn_code(RETURN_CODE_UNKNOWN);
			xpbisbomO.setRtn_mesg("找不到成品型号:" + mdl_id_fk);
			return xpbisbomO;
		}
//		bis_mdl_def.setCus_id(bis_mtrl.getMtrl_mkr());
		bisMdlDefDao.update(bis_mdl_def);

		xpbisbomO.setRtn_code(RETURN_CODE_OK);
		xpbisbomO.setRtn_mesg("success");
		return xpbisbomO;
	}

	public XpbisbomO updateFnc() {
		XpbisbomO xpbisbomO = new XpbisbomO();
		xpbisbomO.setTrx_id("XPBISBOM");
		xpbisbomO.setType_id("O");
		XpbisbomIA iary = xpbisbomI.getIary().get(0);
		String mtrl_prod_id_fk = iary.getMtrl_prod_id_fk();
		String mdl_id_fk = iary.getMdl_id_fk();
		Bis_bomId id = new Bis_bomId();
		id.setMdl_id_fk(iary.getMdl_id_fk());
		id.setMtrl_prod_id_fk(iary.getMtrl_prod_id_fk());
		id.setMdl_mtrl_seq_id(iary.getMdl_mtrl_seq_id());
		Bis_bom bis_bom = bisBomDao.getDataByKeyProc(id);
		if (bis_bom == null) {
			xpbisbomO.setRtn_code(E_XPBISBOM_NOT_FOUND_RECORDER + "");
			xpbisbomO.setRtn_mesg("not Found Record[BIS_BOM]");
			return xpbisbomO;
		}
		if (iary.getOpe_id_fk() != null) {
			bis_bom.setOpe_id_fk(iary.getOpe_id_fk());
		}
		if (iary.getMtrl_unit_fk() != null) {
			bis_bom.setMtrl_unit_fk(iary.getMtrl_unit_fk());
		}
		if (iary.getCns_flg() != null) {
			bis_bom.setCns_flg(iary.getCns_flg());
		}
		if (iary.getMnt_flg() != null) {
			bis_bom.setMnt_flg(iary.getMnt_flg());
		}
		if (iary.getMtrl_use_qty() != null) {
			bis_bom.setMtrl_use_qty(iary.getMtrl_use_qty());
		}
		bisBomDao.save(bis_bom);

		Bis_mtrl bis_mtrl = bisMtrlDao.get(mtrl_prod_id_fk);
		if (bis_mtrl == null) {
			xpbisbomO.setRtn_code(RETURN_CODE_UNKNOWN);
			xpbisbomO.setRtn_mesg("找不到料号:" + mtrl_prod_id_fk);
			return xpbisbomO;
		}
		Bis_mdl_def bis_mdl_def = bisMdlDefDao.get(mdl_id_fk);
		if (bis_mdl_def == null) {
			xpbisbomO.setRtn_code(RETURN_CODE_UNKNOWN);
			xpbisbomO.setRtn_mesg("找不到成品型号:" + mdl_id_fk);
			return xpbisbomO;
		}
//		bis_mdl_def.setCus_id(bis_mtrl.getMtrl_mkr());
		bisMdlDefDao.update(bis_mdl_def);

		xpbisbomO.setRtn_code(RETURN_CODE_OK);
		xpbisbomO.setRtn_mesg("success");
		return xpbisbomO;
	}

	public XpbisbomO deleteFnc() {
		XpbisbomO xpbmdbomO = bisBomDao.deleteProc(xpbisbomI);
		return xpbmdbomO;
	}

	public String comObj2String(Object obj) {
		if (obj != null) {
			return obj.toString();
		}
		return null;
	}

	public XpbisbomO queryByConditionFnc() {
		//		bisBomDao.queryByCondition(xpbisbomI);
		/***
		 * 暂只支持 by mdl_id_fk 查询
		 * **/

		XpbisbomO xpbisbomO = new XpbisbomO();
		xpbisbomO.setTrx_id("XPBISBOM");
		xpbisbomO.setType_id("O");
		List<XpbisbomOA> oaryList = new ArrayList<XpbisbomOA>();

		XpbisbomIA iary = xpbisbomI.getIary().get(0);
		String mdlId = iary.getMdl_id_fk();

		String hql = "FROM Bis_bom where mdl_id_fk = ?";
		List<Bis_bom> bisBomList = bisBomDao.list(hql, mdlId);
		for(Bis_bom bisBom : bisBomList){
			String mtrlProdId = bisBom.getId().getMtrl_prod_id_fk();
			Bis_mtrl bisMtrl = bisMtrlDao.get(mtrlProdId);
			XpbisbomOA oary = new XpbisbomOA();
			oary.setMdl_id_fk(mdlId);
			oary.setMtrl_prod_id_fk(bisMtrl.getMtrl_prod_id());
			oary.setMtrl_cate(mtrlProdId);
			oary.setOpe_id_fk(bisBom.getOpe_id_fk());
			oary.setMtrl_use_qty(bisBom.getMtrl_use_qty());
			oary.setCns_flg(bisBom.getCns_flg());
			oary.setMnt_flg(bisBom.getMnt_flg());
			oary.setMtrl_prod_dsc(bisMtrl.getMtrl_prod_dsc());
			oary.setMtrl_unit_fk(bisBom.getMtrl_unit_fk());
			oary.setMdl_mtrl_seq_id(bisBom.getId().getMdl_mtrl_seq_id());
			hql = "FROM Bis_data where data_cate = ? and ext_1 = ?";
			String mtrl_mkr = "";
			List<Bis_data> bisDataList = bisDataDao.list(hql,"CSMT",mtrlProdId);
			for(Bis_data bisData : bisDataList){
				mtrl_mkr += bisData.getData_item();
				mtrl_mkr += (bisDataList.indexOf(bisData)== bisDataList.size() -1) ? "":",";
			}	
			oary.setMtrl_mkr(mtrl_mkr);
			oaryList.add(oary);
		}
				
//				"SELECT mdl_id_fk,mtrl_prod_id_fk,mtrl_cate,ope_id_fk,mtrl_use_qty,"
//				+ "cns_flg,mnt_flg,mtrl_mkr,mtrl_prod_dsc,mtrl_unit_fk,mdl_mtrl_seq_id "
//				+ "FROM BIS_MTRL A ,BIS_BOM B where A.mtrl_prod_id=B.mtrl_prod_id_fk "
//				+ "and mdl_id_fk='" + iary.getMdl_id_fk() + "'";
//		List<Object[]> bomList = bisBomDao.findBySQL(hql);
//		for (Object bomItem[] : bomList) {
//			XpbisbomOA oary = new XpbisbomOA();
//
//			oary.setMdl_id_fk(comObj2String(bomItem[0]));
//			oary.setMtrl_prod_id_fk(comObj2String(bomItem[1]));
//			oary.setMtrl_cate(comObj2String(bomItem[2]));
//			oary.setOpe_id_fk(comObj2String(bomItem[3]));
//			oary.setMtrl_use_qty(comObj2String(bomItem[4]));
//			oary.setCns_flg(comObj2String(bomItem[5]));
//			oary.setMnt_flg(comObj2String(bomItem[6]));
//			oary.setMtrl_mkr(comObj2String(bomItem[7]));
//			oary.setMtrl_prod_dsc(comObj2String(bomItem[8]));
//			oary.setMtrl_unit_fk(comObj2String(bomItem[9]));
//			oary.setMdl_mtrl_seq_id(comObj2String(bomItem[10]));
//
//			oaryList.add(oary);
//		}
		xpbisbomO.setOary(oaryList);
		xpbisbomO.setTbl_cnt(oaryList.size());
		xpbisbomO.setRtn_code(RETURN_CODE_OK);
		xpbisbomO.setRtn_mesg(RETURN_MESG_OK);
		return xpbisbomO;
	}

	public void queryByMtrl() {
		xpbisbomO = bisBomDao.queryByCondition(xpbisbomI);
	}

	public XpbisbomO errActionFnc() {
		XpbisbomO xpbisbomO = new XpbisbomO();
		xpbisbomO.setTrx_id("XPLSTDAT");
		xpbisbomO.setType_id("O");
		xpbisbomO.setRtn_code("0000009");
		xpbisbomO.setRtn_mesg("Invalid Action_flg");
		return xpbisbomO;
	}

}
